﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Planer.Model;

namespace Planer.UI.Controls
{
    public partial class PeriodicalActionControl : UserControl
    {
        public PeriodicalActionControl()
        {
            InitializeComponent();
        }

        periodical_action periodicalAction = null;
        periodical_action_transanction lastTransaction;

        allocation allocation = null;
        allocation_transanction lastAllocationTransaction;

        public object PeriodicalAction
        {
            get
            {
                if (periodicalAction != null) return periodicalAction;
                return allocation;
            }
            set
            {
                if (value == null) return;
                if (value is periodical_action)
                {
                    periodicalAction = value as periodical_action;   

                    labelActionName.Text = periodicalAction.action_category.name;
                    labelSumm.Text = periodicalAction.amount.ToString("C");
                    if (periodicalAction.amount > 0)
                    {
                        actionType = ActionType.INCOME;
                    }

                    labelPeriod.Text = PeriodicalActionModel.GetInstance().PrettyPeriod(periodicalAction);

                    lastTransaction = PeriodicalActionTransactionModel
                        .GetInstance()
                        .GetLastTransactionByPeriodicalAction(periodicalAction);
                    if (lastTransaction == null)
                    {
                        if (periodicalAction.apply_date <= DateTime.Now)
                        {
                            labelNextDate.Text = PeriodModel.PrettyDate(PeriodModel
                                .CreateDateFromDateAndPeriod(periodicalAction.apply_date, periodicalAction));
                        }
                        else
                        {
                            labelNextDate.Text = PeriodModel.PrettyDate(periodicalAction.apply_date);
                        }
                    }
                    else
                    {
                        labelNextDate.Text = PeriodModel.PrettyDate(PeriodModel
                            .CreateDateFromDateAndPeriod(lastTransaction.mustbe_date, periodicalAction));
                    }

                }
                else if (value is allocation)
                {
                    allocation = value as allocation;
                    if (allocation.financial_goal != null)
                    {
                        labelActionName.Text = "Отчисления на " + allocation.financial_goal.name;
                    }
                    else
                    {
                        labelActionName.Text = "Отчисления на " + allocation.accumulation.name;
                    }
                    labelSumm.Text = allocation.amount.ToString("C");                    
                    actionType = ActionType.OUTCOME;                

                    labelPeriod.Text = PeriodicalActionModel.GetInstance().PrettyPeriod(allocation);

                    lastAllocationTransaction = AllocationTransactionModel
                        .GetInstance()
                        .GetLastTransactionByAllocation(allocation);
                    if (lastAllocationTransaction == null)
                    {
                        if (allocation.apply_date <= DateTime.Now)
                        {
                            labelNextDate.Text = PeriodModel.PrettyDate(PeriodModel
                                .CreateDateFromDateAndPeriod(allocation.apply_date, allocation));
                        }
                        else
                        {
                            labelNextDate.Text = PeriodModel.PrettyDate(allocation.apply_date);
                        }
                    }
                    else
                    {
                        labelNextDate.Text = PeriodModel.PrettyDate(PeriodModel
                            .CreateDateFromDateAndPeriod(lastAllocationTransaction.mustbe_date, allocation));
                    }
                }                               
            }
        }
        ActionType actionType = ActionType.OUTCOME;



        private void buttonManualTransaction_Click(object sender, EventArgs e)
        {
            DateTime date;
            ModelResult mr = null;

            if (periodicalAction != null)
            {
                if (lastTransaction != null)
                {
                    date = PeriodModel
                                .CreateDateFromDateAndPeriod(lastTransaction.mustbe_date, periodicalAction);
                }
                else
                {
                    date = PeriodModel
                                .CreateDateFromDateAndPeriod(periodicalAction.apply_date, periodicalAction);
                }
                periodical_action_transanction transaction = new periodical_action_transanction()
                {
                    create_date = DateTime.Now,
                    mustbe_date = date,
                    periodical_action = periodicalAction,
                    period = PeriodModel.GetInstance().GetCurrentPeriod()
                };

                mr = BillingModel.GetInstance().MoneyOperation(periodicalAction.billing, transaction);
            }
            else if (allocation != null)
            {
                if (lastAllocationTransaction != null)
                {
                    date = PeriodModel
                                .CreateDateFromDateAndPeriod(lastAllocationTransaction.mustbe_date, allocation);
                }
                else
                {
                    date = PeriodModel
                                .CreateDateFromDateAndPeriod(allocation.apply_date, allocation);
                }
                allocation_transanction allocTransaction = new allocation_transanction()
                {
                    allocation = allocation,
                    create_date = DateTime.Now,
                    mustbe_date = date,
                    period = PeriodModel.GetInstance().GetCurrentPeriod()
                };

                mr = BillingModel.GetInstance().MoneyOperation(allocation.billing, allocTransaction);
            }
            else { return; }
            if (mr != null && mr.Status == OperationStatusType.ERROR)
            {
                MessageBoxUtils.ShowErrorBox(mr.ShowAllMessages);
                return;
            }
            Program.MainForm.RefreshBillControl();
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            string type;
            if(actionType == ActionType.INCOME)
                type = "доход";
            else
                type = "расход";
            if (MessageBoxUtils.Question("Вы действительно хотите удалить " + type) == DialogResult.No)
            {
                return;
            }
            ModelResult mr = null;
            if (periodicalAction != null)
            {
                periodicalAction.is_active = "n";
                mr = PeriodicalActionModel.GetInstance().SaveOrUpdatePeriodicalAction(periodicalAction);
            }
            else
            {
                allocation.is_active = "n";
                mr = AllocationModel.GetInstance().SaveOrUpdateAllocation(allocation);
            }
            if (mr.Status == OperationStatusType.ERROR)
            {
                MessageBoxUtils.ShowErrorBox(mr.ShowAllMessages);
                return;
            }
            Program.MainForm.RefreshBillControl();
        }
    }
}
